/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.wicp.tams.common.flink.sink.influxdb.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import net.wicp.tams.common.flink.sink.influxdb.InfluxDBConfig;
import net.wicp.tams.common.flink.sink.influxdb.InfluxDBPoint;
import net.wicp.tams.common.flink.sink.influxdb.InfluxDBSink;

public class InfluxDBSinkExample {
	private static final int N = 10000;

	public static void main(String[] args) throws Exception {
		final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

		List<String> dataList = new ArrayList<>();
		for (int i = 0; i < N; ++i) {
			String id = "server" + String.valueOf(i);
			dataList.add("cpu#" + id);
			dataList.add("mem#" + id);
			dataList.add("disk#" + id);
		}
		DataStream<String> source = env.fromElements(dataList.toArray(new String[0]));

		DataStream<InfluxDBPoint> dataStream = source.map(new RichMapFunction<String, InfluxDBPoint>() {
			@Override
			public InfluxDBPoint map(String s) throws Exception {
				String[] input = s.split("#");

				String measurement = input[0];
				long timestamp = System.currentTimeMillis();

				HashMap<String, String> tags = new HashMap<>();
				tags.put("host", input[1]);
				tags.put("region", "region#" + String.valueOf(input[1].hashCode() % 20));

				HashMap<String, Object> fields = new HashMap<>();
				fields.put("value1", input[1].hashCode() % 100);
				fields.put("value2", input[1].hashCode() % 50);

				return new InfluxDBPoint(measurement, timestamp, tags, fields);
			}
		});

		// dataStream.print();
		InfluxDBConfig influxDBConfig = InfluxDBConfig.builder("http://localhost:8086", "db_flink_test")
				.batchActions(1000).flushDuration(100, TimeUnit.MILLISECONDS).createDatabase(true).enableGzip(true)
				.build();

		dataStream.addSink(new InfluxDBSink(influxDBConfig));

		env.execute("InfluxDB Sink Example");
	}

}
